package com.erpanderp.wam.tm.view;

import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import com.erpanderp.wam.tm.model.entity.RRFSelectedSkillsList;
import com.erpanderp.wam.tm.model.entity.RecruitmentProfile;
import com.erpanderp.wam.tm.model.entity.RecruitmentRequest;
import com.erpanderp.wam.tm.model.entity.SkillRating;
import com.erpanderp.wam.tm.model.service.RRFDAO;
import com.erpanderp.wam.tm.model.service.RRFSelectedSkillsListDAO;
import com.erpanderp.wam.tm.model.service.RecruitmentProfileDAO;
import com.erpanderp.wam.tm.model.service.SkillRatingDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class CreateFeedBackBean implements Serializable {

	private static final long serialVersionUID = 1L;
	HttpServletRequest request = (HttpServletRequest) FacesContext
			.getCurrentInstance().getExternalContext().getRequest();

	private String rrfCode;
	private String candidateEmail;
	private boolean FORM_VALID;
	private boolean firstLevelInterview;
	private boolean secondLevelInterview;
	
	private String strengths;
	private String areaOfImprovement;
	private String intwrFinalRemarks;
	private String intwr1FinaRecommendation;

	private RRFSelectedSkillsListDAO rrfSelectedSkillsListDAO = new RRFSelectedSkillsListDAO();
	private RecruitmentProfileDAO recruitmentProfileDAO = new RecruitmentProfileDAO();
	private RRFDAO rrfDAO = new RRFDAO();
	private SkillRatingDAO skillRatingDAO = new SkillRatingDAO();

	private List<RRFSelectedSkillsList> rrfSelectedSkillsLists = new ArrayList<RRFSelectedSkillsList>();
	private List<SkillRating> skillRatings = new ArrayList<SkillRating>();

	private RecruitmentRequest recruitmentRequest = new RecruitmentRequest();
	private RecruitmentProfile recruitmentProfile = new RecruitmentProfile();
	private SkillRating skillRating = new SkillRating();
	private RRFSelectedSkillsList rrfSelectedSkillsList=new RRFSelectedSkillsList();
	
	private Date dateAfterFourMnths;

	public CreateFeedBackBean() {
		
		rrfCode = request.getParameter("rrfCode");
		candidateEmail = request.getParameter("candidateEmail");
		
		rrfSelectedSkillsLists = rrfSelectedSkillsListDAO.getSelectedSkillsByRRFCode(rrfCode);
		
		recruitmentRequest = rrfDAO.getRRFByRecruitmentRequestCode(rrfCode);
		recruitmentProfile = recruitmentProfileDAO.getRecruitmentProfileByCandidateEmail(candidateEmail);
		
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formate= new SimpleDateFormat("dd/MM/yyyy");

		currentDate.setTime(recruitmentRequest.getRequestDate());
		
		//get after two months
		currentDate.add(Calendar.MONTH,4);
		
		System.out.println("===========1111111111=============="+recruitmentRequest.getRequestDate());
		try {
			//requestDate = (Date)formate.parse(toDay);
			dateAfterFourMnths = (Date)formate.parse(formate.format(currentDate.getTime()));
			System.out.println("===========dateAfterFourMnths=============="+dateAfterFourMnths);
		} catch (ParseException e) {
			e.printStackTrace();
		} 
	

		for (RRFSelectedSkillsList rrfelectedSkillsList : rrfSelectedSkillsLists) {			
			
			rrfSelectedSkillsList = rrfSelectedSkillsListDAO.getRRFSelectedSkillsListBySkillId(rrfelectedSkillsList.getRrfSelectedSkillsListId());
			
			skillRating = new SkillRating();
			skillRating.setRecruitmentRequestCode(rrfCode);
			skillRating.setRecruitmentProfile(recruitmentProfile);
			skillRating.setRrfSelectedSkill(rrfSelectedSkillsList);					
			
			if(recruitmentRequest.getInterviewLevel().equalsIgnoreCase("Two Level")){
				System.out.println("=============INSIDE TWO LEVEL==========");
				firstLevelInterview= skillRatingDAO.isInterviewLevelFirstOrSecondLevel(rrfCode, recruitmentProfile.getRecruitmentProfileId(), "Single Level");
				if(firstLevelInterview){					
					skillRating.setInterviewLevel("Two Level");
					System.out.println("=====INSIDE IF========"+skillRating.getInterviewLevel());
				}else{
					skillRating.setInterviewLevel("Single Level");
					System.out.println("=====INSIDE ELSE PART========"+skillRating.getInterviewLevel());
				}
				
			}
			
			if(recruitmentRequest.getInterviewLevel().equalsIgnoreCase("Single Level")){
				System.out.println("=============INSIDE SINGLE LEVEL==========");
				skillRating.setInterviewLevel("Single Level");
			}
			
			System.out.println("skillRating.getInterviewLevel()-----Finally----"+skillRating.getInterviewLevel());
			skillRating.setCreateDate(new Date());
			skillRating.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
			skillRating.setStatus(CommonLookup.STATUS_ACTIVE);
			
			skillRatings.add(skillRating);
		}
		
	}

	// SAVE SKILL RATINGS
	public String saveSkillRatings() {
		validateForm();
		if (FORM_VALID) {
			
			if(recruitmentRequest.getInterviewLevel().equalsIgnoreCase("Two Level") && recruitmentRequest.getInterviewerFBLevel().equalsIgnoreCase("Level1") ){
				System.out.println("=============LOOP1================");
				recruitmentRequest.setInterviewerFBLevel("Level2");
				recruitmentProfile.setInterviewer1FeedbackStatus("Available");	
				
				recruitmentProfile.setIntwr1Strengths(strengths);
				recruitmentProfile.setIntwr1AreaOfImprovement(areaOfImprovement);
				recruitmentProfile.setIntwr1FinalRemarks(intwrFinalRemarks);
				recruitmentProfile.setIntwr1FinalRecommendation(intwr1FinaRecommendation);
				
			}else if(recruitmentRequest.getInterviewLevel().equalsIgnoreCase("Two Level") && recruitmentRequest.getInterviewerFBLevel().equalsIgnoreCase("Level2-Finished") ){
				System.out.println("=============LOOP22222222================");
				recruitmentProfile.setIntwr2Strengths(strengths);
				recruitmentProfile.setIntwr2AreaOfImprovement(areaOfImprovement);
				recruitmentProfile.setIntwr2FinalRemarks(intwrFinalRemarks);
				recruitmentProfile.setIntwr2FinalRecommendation(intwr1FinaRecommendation);
				
				recruitmentProfile.setInterviewer2FeedbackStatus("Available");
				
			}else if(recruitmentRequest.getInterviewLevel().equalsIgnoreCase("Single Level") && recruitmentRequest.getInterviewerFBLevel().equalsIgnoreCase("Level1") ){
				System.out.println("=============LOOP333333333333================");	
				recruitmentProfile.setInterviewer1FeedbackStatus("Available");
				recruitmentProfile.setIntwr1Strengths(strengths);
				recruitmentProfile.setIntwr1AreaOfImprovement(areaOfImprovement);
				recruitmentProfile.setIntwr1FinalRemarks(intwrFinalRemarks);
				recruitmentProfile.setIntwr1FinalRecommendation(intwr1FinaRecommendation);
				
			}
		
			rrfDAO.updateRRFRequest(recruitmentRequest);			
					System.out.println("=================Before Skill Rating=============");
			skillRatingDAO.createSkillRatings(skillRatings);
					System.out.println("=================After Skill Rating=============");	
			recruitmentProfileDAO.saveRecruitmentProfile(recruitmentProfile);
			
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO,
							" Your Feedback is submitted successfully.", ""));
			//recruitmentRequest = new RecruitmentRequest();
			
			Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

			  String rrfCode = params.get("rrfCode");
			  String activity=params.get("activity");
			  
			  System.out.println("activity:::::::===================:::"+activity);
			  
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("rrfCode", rrfCode);		  
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("activity", activity);
			
			  try {
				FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/tm/scheduleinterviews.html?rrfCode="+rrfCode+"&activity="+activity);
			} catch (IOException e) {
				e.printStackTrace();
			}
			  
			  return "scheduleinterviews";
		}
		
		return null;
	}

	// VALIDATIONS CHECK
	private void validateForm() {
		FORM_VALID = true;

		if (ValidationUtil.isEmpty(intwrFinalRemarks,
				"Remarks / Overall Impression", null,
				FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}
		if (ValidationUtil.isEmpty(intwr1FinaRecommendation,
				"Final Recommendation ", null,
				FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty((recruitmentProfile.getExpectedJoiningDate() !=null) ? recruitmentProfile.getExpectedJoiningDate().toString():null, "Expected Joining Date", null, FacesContext.getCurrentInstance()) != null){
			
			   FORM_VALID = false;
		} 
		 if(recruitmentProfile.getExpectedJoiningDate() != null){
			   if(recruitmentProfile.getExpectedJoiningDate().before(recruitmentRequest.getRequestDate())){
				   FORM_VALID = false;
				   FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Expected date of joining should be greater than rrf create date", ""));
			   }
			   if(recruitmentProfile.getExpectedJoiningDate().after(dateAfterFourMnths)){
				   FORM_VALID = false;
				   FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Expected date of joining should not be exceed 4 months (or) 120 Days", ""));
			   }
		  }

		System.out.println("Finally FORM_VALID is :::::" + FORM_VALID);
	}

	public String getRrfCode() {
		return rrfCode;
	}

	public void setRrfCode(String rrfCode) {
		this.rrfCode = rrfCode;
	}

	public RRFSelectedSkillsListDAO getRrfSelectedSkillsListDAO() {
		return rrfSelectedSkillsListDAO;
	}

	public void setRrfSelectedSkillsListDAO(
			RRFSelectedSkillsListDAO rrfSelectedSkillsListDAO) {
		this.rrfSelectedSkillsListDAO = rrfSelectedSkillsListDAO;
	}


	public RRFDAO getRrfDAO() {
		return rrfDAO;
	}

	public void setRrfDAO(RRFDAO rrfDAO) {
		this.rrfDAO = rrfDAO;
	}

	public RecruitmentRequest getRecruitmentRequest() {
		return recruitmentRequest;
	}

	public void setRecruitmentRequest(RecruitmentRequest recruitmentRequest) {
		this.recruitmentRequest = recruitmentRequest;
	}

	public RecruitmentProfileDAO getRecruitmentProfileDAO() {
		return recruitmentProfileDAO;
	}

	public void setRecruitmentProfileDAO(
			RecruitmentProfileDAO recruitmentProfileDAO) {
		this.recruitmentProfileDAO = recruitmentProfileDAO;
	}

	public RecruitmentProfile getRecruitmentProfile() {
		return recruitmentProfile;
	}

	public void setRecruitmentProfile(RecruitmentProfile recruitmentProfile) {
		this.recruitmentProfile = recruitmentProfile;
	}

	public SkillRating getSkillRating() {
		return skillRating;
	}

	public void setSkillRating(SkillRating skillRating) {
		this.skillRating = skillRating;
	}

	public String getCandidateEmail() {
		return candidateEmail;
	}

	public void setCandidateEmail(String candidateEmail) {
		this.candidateEmail = candidateEmail;
	}

	public boolean isFORM_VALID() {
		return FORM_VALID;
	}

	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}

	public SkillRatingDAO getSkillRatingDAO() {
		return skillRatingDAO;
	}

	public void setSkillRatingDAO(SkillRatingDAO skillRatingDAO) {
		this.skillRatingDAO = skillRatingDAO;
	}

	public List<SkillRating> getSkillRatings() {
		return skillRatings;
	}

	public void setSkillRatings(List<SkillRating> skillRatings) {
		this.skillRatings = skillRatings;
	}



	public List<RRFSelectedSkillsList> getRrfSelectedSkillsLists() {
		return rrfSelectedSkillsLists;
	}

	public void setRrfSelectedSkillsLists(List<RRFSelectedSkillsList> rrfSelectedSkillsLists) {
		this.rrfSelectedSkillsLists = rrfSelectedSkillsLists;
	}

	public RRFSelectedSkillsList getRrfSelectedSkillsList() {
		return rrfSelectedSkillsList;
	}

	public void setRrfSelectedSkillsList(RRFSelectedSkillsList rrfSelectedSkillsList) {
		this.rrfSelectedSkillsList = rrfSelectedSkillsList;
	}

	public boolean isFirstLevelInterview() {
		return firstLevelInterview;
	}

	public void setFirstLevelInterview(boolean firstLevelInterview) {
		this.firstLevelInterview = firstLevelInterview;
	}

	public boolean isSecondLevelInterview() {
		return secondLevelInterview;
	}

	public void setSecondLevelInterview(boolean secondLevelInterview) {
		this.secondLevelInterview = secondLevelInterview;
	}

	public String getStrengths() {
		return strengths;
	}

	public void setStrengths(String strengths) {
		this.strengths = strengths;
	}

	public String getAreaOfImprovement() {
		return areaOfImprovement;
	}

	public void setAreaOfImprovement(String areaOfImprovement) {
		this.areaOfImprovement = areaOfImprovement;
	}

	public String getIntwrFinalRemarks() {
		return intwrFinalRemarks;
	}

	public void setIntwrFinalRemarks(String intwrFinalRemarks) {
		this.intwrFinalRemarks = intwrFinalRemarks;
	}

	public String getIntwr1FinaRecommendation() {
		return intwr1FinaRecommendation;
	}

	public void setIntwr1FinaRecommendation(String intwr1FinaRecommendation) {
		this.intwr1FinaRecommendation = intwr1FinaRecommendation;
	}

}
